package net.tngou.iask.reflect;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import net.tngou.iask.pojo.Accessresult;
import net.tngou.iask.pojo.Health;
import net.tngou.iask.pojo.POJO;
import net.tngou.iask.pojo.Patient;
import net.tngou.iask.pojo.Question;
import net.tngou.iask.service.BaseService;

/**
 * 过滤函数 用于数据配置
 * @author tngou@tngou.net
 *
 */
public class FilterFun {

	
	
	/**
	 * 性别关系  男性、女性
	 * @param patient 患者ID
	 * @param gender 性别 0（未知）、 1（男） 、2 （女） 
	 * @return
	 */
	public boolean gender(long patient,int gender) {
		Patient bean=new Patient().get(patient);
		if(bean==null)return false;
		if(bean.getGender()==gender)return true;
		return false;		
	}
	
	/**
	 * 唯一不变
	 * @param access 随访
	 * @param question 问题
	 * @param patient 患者
	 * @return
	 */
	public boolean unique(long access,long question,long patient) {
		Patient p=new Patient().get(patient);
		Question q=new Question().get(question);
		if(p==null||q==null)return true;
		BaseService service = new BaseService();
		@SuppressWarnings("unchecked")
		List<Health> list = (List<Health>) service.getList("patient="+patient+" AND question="+question,Health.class);
		if(list!=null&&list.size()>0)
		{			
			for (Health health : list) {		
					POJO bean = new Accessresult().get("access="+access+" AND  question="+question);
					if(bean==null)
					{
						try {
							Accessresult result= new Accessresult();
							BeanUtils.copyProperties(result, health);
							result.setAccess(access);
							result.setId(0);
							result.save();
						} catch (IllegalAccessException | InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							return true;
						}
					}
				
			}
			return false;
			
		}
		return true;
	}
	
	
	/**
	 * 根据上个问题,是否显示
	 * @param access 访问
	 * @param question 问题
	 * @param patient 患者
	 * @param value 值
	 * @return
	 */
	public boolean condition(long access,long question,int value) {
		
		POJO bean = new Accessresult().get("access="+access+" AND question="+question+" AND value="+value);
		if(bean!=null)return true;
		return false;
		
	}
	
}
